import 'package:flutter/material.dart';
import 'package:flutter_app/widgets/check_button.dart';

class RoomConfigurationItem {
  final String title;
  final int icon;
  final bool isChecked;

  RoomConfigurationItem(this.title, this.icon, this.isChecked);
}

List _dataList = [
  RoomConfigurationItem('衣柜', 0xe60d, false),
  RoomConfigurationItem('宽带', 0xe6ba, false),
  RoomConfigurationItem('天然气', 0xe60e, false),
  RoomConfigurationItem('空调', 0xe63c, false),
  RoomConfigurationItem('电视机', 0xe90a, false),
  RoomConfigurationItem('冰箱', 0xe90b, false),
  RoomConfigurationItem('沙发', 0xe61d, false),
  RoomConfigurationItem('洗衣机', 0xec78, false),
  RoomConfigurationItem('热水器', 0xe6bc, false),
  RoomConfigurationItem('暖气', 0xe67d, false),
];

/// @description: 房屋配置

class RoomConfiguration extends StatefulWidget {
  const RoomConfiguration({
    Key? key,
    required this.onChange,
  }) : super(key: key);
  final ValueChanged onChange;

  @override
  State<RoomConfiguration> createState() => _RoomConfigurationState();
}

class _RoomConfigurationState extends State<RoomConfiguration> {
  List _list = _dataList;
  @override
  Widget build(BuildContext context) {
    return Wrap(
      // spacing: 10,
      runSpacing: 20,
      children: _list
          .map((e) => GestureDetector(
                onTap: () {
                  // 点击改变状态
                  setState(() {
                    _list = _list
                        .map((item) => item == e
                            ? RoomConfigurationItem(
                                e.title, e.icon, !e.isChecked)
                            : item)
                        .toList();
                  });
                  //  同时通知父级
                  widget.onChange(_list);
                },
                behavior: HitTestBehavior.translucent,
                child: SizedBox(
                  child: Column(
                    children: [
                      //外部字体图标的使用
                      Icon(IconData(e.icon, fontFamily: 'MyIcons')),
                      Text(e.title),
                      CheckButton(
                        isChecked: e.isChecked,
                      )
                    ],
                  ),
                  width: MediaQuery.of(context).size.width / 5,
                ),
              ))
          .toList(),
    );
  }
}

class HouseConfigurationList extends StatelessWidget {
  const HouseConfigurationList({Key? key, this.list}) : super(key: key);
  final List<String>? list;
  @override
  Widget build(BuildContext context) {
    // 筛选数据where
    var showList =
        _dataList.where((element) => list!.contains(element.title)).toList();
    if (showList.isEmpty) {
      return Container(
        padding: const EdgeInsets.only(
          left: 10.0,
        ),
        child: const Text('暂无房屋配置信息'),
      );
    }
    return Wrap(
      runSpacing: 20,
      children: showList
          .map((e) => SizedBox(
                child: Column(
                  children: [
                    //外部字体图标的使用
                    Icon(IconData(e.icon, fontFamily: 'MyIcons')),
                    Text(e.title),
                  ],
                ),
                width: MediaQuery.of(context).size.width / 5,
              ))
          .toList(),
    );
  }
}
